home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / FREENET / MELL / NETLIB00 / NetLib / c / net < prev    next >
Text File  |  1995-02-28  |  4KB  |  211 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "arpa/inet.h"
  6. #include "netdb.h"
  7. #include "netinet/in.h"
  8. #include "sys/socket.h"
  9.  
  10. #include "socketlib.h"
  11.  
  12. /*
  13.  * File handle for the nets file
  14.  */
  15. static FILE *netfile = NULL;
  16.  
  17. /*
  18.  * Keep the file open between calls to these routines?
  19.  */
  20. static int keepopen = 0;
  21.  
  22. /*
  23.  * Local routines
  24.  */
  25. static int __setnetent(int allowrewind);
  26. static struct netent *__getnetent(void);
  27.  
  28. /*
  29.  * Open and rewind the nets file
  30.  */
  31. int setnetent(int stayopen)
  32. {
  33.   /* Record whether the file should be kept open */
  34.   keepopen = stayopen;
  35.  
  36.   return __setnetent(1);
  37. }
  38.  
  39. /*
  40.  * Do the real work of opening/rewinding the nets file
  41.  */
  42. static int __setnetent(int allowrewind)
  43. {
  44.   /* Open or rewind the file as necessary */
  45.   if (netfile) {
  46.     if (allowrewind)
  47.       rewind(netfile);
  48.   } else {
  49.     netfile = fopen("InetDBase:Networks", "r");
  50.   }
  51.  
  52.   return (netfile == NULL) ? -1 : 0;
  53. }
  54.  
  55. /*
  56.  * Fetch the next entry from the nets file
  57.  */
  58. struct netent *getnetent()
  59. {
  60.   struct netent *net;
  61.  
  62.   /* Open the file if necessary */
  63.   if (netfile == NULL)
  64.     if (__setnetent(0) == -1)
  65.       return NULL;
  66.  
  67.   /* Do the actual read */
  68.   net = __getnetent();
  69.  
  70.   /* Close the file unless the user has prohibited it */
  71.   if (!keepopen)
  72.     endnetent();
  73.  
  74.   return net;
  75. }
  76.  
  77. /*
  78.  * Do the real work of getting an entry from the file
  79.  */
  80. struct netent *__getnetent()
  81. {
  82.   static struct netent net = {
  83.     NULL, NULL, AF_INET, 0
  84.   };
  85.  
  86.   char **item;
  87.   char *line;
  88.   char *element;
  89.   int  aliases;
  90.  
  91.   /* Free up any memory in use */
  92.   if (net.n_name)
  93.   {
  94.     for (item = net.n_aliases; *item; item++)
  95.       free(*item);
  96.     free(net.n_name);
  97.     free(net.n_aliases);
  98.  
  99.     net.n_name = NULL;
  100.   }
  101.  
  102.   /* Read a line from the file */
  103.   if ((line = __socketlib_readline(netfile)) == NULL)
  104.     return NULL;
  105.  
  106.   /* Extract the offical network name from the line */
  107.   element = strtok(line, " \t");
  108.   net.n_name = strdup(element);
  109.  
  110.   /* Extract the address from the line */
  111.   element = strtok(NULL, " \t");
  112.   net.n_net = inet_network(element);
  113.  
  114.   /* Initialialise the alias list */
  115.   net.n_aliases = malloc(sizeof(char *));
  116.   net.n_aliases[0] = NULL;
  117.   aliases = 1;
  118.  
  119.   /* Extract the aliases */
  120.   while ((element = strtok(NULL, " \t")) != NULL)
  121.   {
  122.      aliases += 1;
  123.      net.n_aliases = realloc(net.n_aliases, aliases * sizeof(char *));
  124.      net.n_aliases[aliases-2] = strdup(element);
  125.      net.n_aliases[aliases-1] = NULL;
  126.   }
  127.  
  128.   return &net;
  129. }
  130.  
  131. /*
  132.  * Close the nets file
  133.  */
  134. int endnetent()
  135. {
  136.   int status = 0;
  137.  
  138.   /* If its open, close it */
  139.   if (netfile) {
  140.     status = fclose(netfile);
  141.     netfile = 0;
  142.   }
  143.  
  144.   return status;
  145. }
  146.  
  147. /*
  148.  * Search the nets file for a given net name
  149.  */
  150. struct netent *getnetbyname(const char *name)
  151. {
  152.   struct netent *net;
  153.   char          **alias;
  154.  
  155.   /* Open/rewind the file */
  156.   if (__setnetent(1) == -1)
  157.     return NULL;
  158.  
  159.   /* Look through the file for a match */
  160.   while ((net = __getnetent()) != NULL) {
  161.  
  162.     /* Does the offical name match? */
  163.     if (strcmp(net->n_name, name) == 0)
  164.       break;
  165.  
  166.     /* Do any of the aliases match? */
  167.     for (alias = net->n_aliases; *alias; alias++)
  168.     {
  169.       if (strcmp(*alias, name) == 0)
  170.         break;
  171.     }
  172.  
  173.     /* Did any of the aliases match? */
  174.     if (*alias)
  175.       break;
  176.   }
  177.  
  178.   /* Close the file unless the user has prohibited it */
  179.   if (!keepopen)
  180.     endnetent();
  181.  
  182.   return net;
  183. }
  184.  
  185. /*
  186.  * Search the nets file for a given address
  187.  */
  188. struct netent *getnetbyaddr(long netaddr, int type)
  189. {
  190.   struct netent *net;
  191.  
  192.   /* Open/rewind the file */
  193.   if (__setnetent(1) == -1)
  194.     return NULL;
  195.  
  196.   /* Look through the file for a match */
  197.   while ((net = __getnetent()) != NULL) {
  198.  
  199.     /* If the type and length match, we've found it */
  200.     if ((net->n_addrtype == type) && (net->n_net == netaddr))
  201.       break;
  202.  
  203.   }
  204.  
  205.   /* Close the file unless the user has prohibited it */
  206.   if (!keepopen)
  207.     endnetent();
  208.  
  209.   return net;
  210. }
  211.